package cn.test.dao.impl;

import cn.test.dao.CRUDTemplate;
import cn.test.dao.CustInfoDao;
import cn.test.entity.CusInfo;
import cn.test.entity.impl.BeanHandler;
import cn.test.entity.impl.BeanListHandler;

import java.util.List;

public class CustInfoDaoImpl implements CustInfoDao {
    //增
    @Override
    public void insert(CusInfo cusInfo){
        String sql="insert into cusinfo(uid,name,gender,age,phone,mail)"
                + "values ('"+cusInfo.getUid()+"','"+cusInfo.getName()+"','"+cusInfo.getGender()+"','"+cusInfo.getAge()+"','"+cusInfo.getPhone()+"','"+cusInfo.getMail()+"')";
        int status = CRUDTemplate.executeUpdate(sql);
        if (status>0) {
            System.out.println("添加成功！");
        }
    }
    //删
    @Override
    public void del(CusInfo cusInfo){
        String sql = "delete from cusinfo where uid="+cusInfo.getUid();
        int status = CRUDTemplate.executeUpdate(sql);
        if (status>0) {
            System.out.println("删除成功");
        }
    }
    //改
    @Override
    public void mod(CusInfo cusInfo){
        StringBuffer sql=new StringBuffer("update cusinfo set");
        if (cusInfo.getName() != null) {
            sql.append(" name="+"\""+cusInfo.getName()+"\",");
        }
        if(cusInfo.getGender()!=null){
            sql.append(" gender="+"\""+cusInfo.getGender()+"\",");
        }
        if(cusInfo.getAge()>0){
            sql.append(" age="+cusInfo.getAge()+",");
        }
        if(cusInfo.getPhone()!=null){
            sql.append(" phone="+"\""+cusInfo.getPhone()+"\",");
        }
        if(cusInfo.getMail()!=null){
            sql.append(" mail="+"\""+cusInfo.getMail()+"\",");
        }
        sql.append("where uid=" + cusInfo.getUid());
        int i = sql.lastIndexOf(",where");
        sql.replace(i,i+6," where");
        int status = CRUDTemplate.executeUpdate(sql.toString());
        if (status>0) {
            System.out.println("修改成功");
        }
    }
    //查
    @Override
    public CusInfo selectOne(CusInfo cusInfo){
        String sql = "select * from cusinfo where uid=?";
        return CRUDTemplate.executeQuery(sql, new BeanHandler<>(CusInfo.class),cusInfo.getUid());
    }
    //查询所有
    @Override
    public List<CusInfo> selectAll(){
        String sql="select * from cusinfo";
        return CRUDTemplate.executeQuery(sql, new BeanListHandler<>(CusInfo.class));
    }

}
